package com.fu.servlet;

import com.fu.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * @author 付大大
 * @description
 * @create 2021-01-24 20:05
 */
@WebServlet("/listStudentServlet")
public class ListStudentServlet extends HttpServlet {

    /** @noinspection Duplicates*/
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("e:\\stu.txt"));
        ArrayList<Student> studentList = new ArrayList<>();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            Student student = new Student();
            String[] split = line.split(",");
            student.setUsername(split[0]);
            student.setAge(Integer.parseInt(split[1]));
            student.setScore(Integer.parseInt(split[2]));
            studentList.add(student);
        }
        req.getSession().setAttribute("students", studentList);
        resp.sendRedirect("/stu/listStudent.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
